<?php

class KnowledgeController extends Controller
{
	public function actionIndex()
	{
		$uid= Yii::app()->request->getParam('uid');
		$User= User::model()->findByPk($uid);
		
		$Academy = new AcademyName();
		$UserAcademy = new UserAcademy();
		$AcademyList = AcademyName::model()->findAll();
		$AcademyList = CHtml::listData($AcademyList, 'academy_name_id','academy_name');
		
		$UserAcademyList = UserAcademy::model()->findAllByAttributes(array('user_academy_user_id'=>$uid), array('order'=>'user_academy_id ASC'));
		
		$Knowledge = UserAcademy::model()->getKnowledge();
		
		$Speciality = Speciality::model()->findAll();
		$Speciality = CHtml::listData($Speciality, 'speciality_id', 'speciality_name');
		
		$Month = Html::getMonth();
		$Year = Html::getYear();
		
		$Degree = UserAcademy::model()->getDegree();
		
		$Training = UserAcademy::model()->getTypeTraining();
		if(isset($_POST['UserAcademy'])){
			$UserAcademy->attributes = $_POST['UserAcademy'];
			
			if(isset($_POST['monthStart']) && isset($_POST['yearStart'])) {
				$dateStart = "{$_POST['yearStart']}-{$_POST['monthStart']}-01";
				$UserAcademy->user_academy_started_time = $dateStart;
			}
			if(isset($_POST['monthEnd']) && isset($_POST['yearEnd'])) {
				$dateEnd = "{$_POST['yearEnd']}-{$_POST['monthEnd']}-01";
				$UserAcademy->user_academy_end_time = $dateEnd;
			}
			
			$UserAcademy->user_academy_user_id = $uid;
			if($UserAcademy->save()){
			  	Yii::log('luu thanh cong');
			  	Yii::app()->user->setFlash('success','Thêm mới thành công');
			  	$this->refresh();
			}
		}
		$this->render('index', array(
			'AcademyList'=>$AcademyList,
			'Academy'=>$Academy,
			'UserAcademyList'=>$UserAcademyList,
			'Knowledge'=>$Knowledge,
			'UserAcademy'=>$UserAcademy,
			'Speciality'=>$Speciality,
			'Month'=>$Month,
			'Year'=>$Year,
			'Degree'=>$Degree,
			'Training'=>$Training 
		));
	}
	
	//delete academy
	public function actionDel() {
		$aid = Yii::app()->request->getParam('aid');
		$userAcademy = UserAcademy::model()->findByPk($aid);
		//$UserAcademyList = Yii::app()->request->getParam('UserAcademyList');
		
		if (!$userAcademy or !$userAcademy->delete())
			throw new CHttpException('404','Error del academy');
		
		$uid= Yii::app()->request->getParam('uid');
		$UserAcademyList = UserAcademy::model()->findAllByAttributes(array('user_academy_user_id'=>$uid), array('order'=>'user_academy_id ASC'));
		$this->renderPartial('_table', array('UserAcademyList'=>$UserAcademyList), false, true);
	}
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}